package com.cn.hnust.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.cn.hnust.pojo.User;
import com.cn.hnust.service.IUserService;

@Controller
@RequestMapping("/index")
public class IndexController { 
    @Resource  
    private IUserService userService;  
      
    @RequestMapping("/index")  
    public String Index(HttpServletRequest request,Model model){  
    	 
        return "index";   
        
    }  
    
    /** 
     * 初始化 “我的产品”列表 JSP页面,具有分页功能 
     *  
     * @param request 
     * @param model 
     * @return 
     */  
    @RequestMapping(value = "list", method = RequestMethod.GET)  
    public String list(HttpServletRequest request, Model model) {  
        /*// 取得SESSION中的loginUser  
        User loginUser = (User) request.getSession().getAttribute("loginUser");  
        // 判断SESSION是否失效  
        if (loginUser == null || "".equals(loginUser)) {  
            return "redirect:/";  
        } 
        int loginUserId = loginUser.getUserId(); */ 
    	
        //此处的productService是注入的IProductService接口的对象  
        this.userService.showProductsByPage(request, model);  
      
        return "list";  
    } 
    
    @RequestMapping(value = "show", method = RequestMethod.GET)  
    public String show(HttpServletRequest request, Model model) {  
    	int userId = Integer.parseInt(request.getParameter("id")); 
		User user = this.userService.getUserById(userId);
		model.addAttribute("user", user); 
      
        return "show";
    } 
    
    @RequestMapping(value = "add", method = RequestMethod.GET)  
    public String add(HttpServletRequest request, Model model) {  
          
      
        return "add";
    }
    @RequestMapping(value = "add", method = RequestMethod.POST)  
    public String postadd(HttpServletRequest request, Model model) {  
        String myname = request.getParameter("username");
        String mypwd = request.getParameter("password");
        String myage = request.getParameter("age");
        User uinfo  = new User();
        uinfo.setUserName(myname);
        uinfo.setPassword(mypwd);
        uinfo.setAge(Integer.parseInt(myage));
    	this.userService.adduser(uinfo);
    	return "redirect:/index/list";
    }
    
    @RequestMapping(value = "update", method = RequestMethod.GET)  
    public String update(HttpServletRequest request, Model model) {  
    	int userId = Integer.parseInt(request.getParameter("id")); 
		User user = this.userService.getUserById(userId);
		model.addAttribute("user", user);   
      
    	return "update";
    }
    @RequestMapping(value = "update", method = RequestMethod.POST)  
    public String postupdate(HttpServletRequest request, Model model) { 
    	String myid = request.getParameter("id");
    	String myname = request.getParameter("username");
        String mypwd = request.getParameter("password");
        String myage = request.getParameter("age");
        User uinfo  = new User();
        uinfo.setId(Integer.parseInt(myid));
        uinfo.setUserName(myname);
        uinfo.setPassword(mypwd);
        uinfo.setAge(Integer.parseInt(myage));
    	this.userService.updateuser(uinfo);  
      
    	return "redirect:/index/list";
    }
    
    @RequestMapping(value = "delete", method = RequestMethod.GET)  
    public String delete(HttpServletRequest request, Model model) {  
    	int id = Integer.parseInt(request.getParameter("id"));
    	this.userService.deleteuser(id);   
      
    	return "redirect:/index/list";
    }
    
}
